package com.cade.agriculturemall.view.personalCenter.manageAddress;

import android.text.TextUtils;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.model.AddressEntity;
import com.cade.agriculturemall.model.UserInfoEntity;

import java.util.List;

/**
 * Created by Lobby on 2017/12/21.
 */

public class ManageAddressPresenter extends BasePresenter<ManageAddressContract.IView> implements ManageAddressContract.IPresenter {
    public ManageAddressPresenter(ManageAddressContract.IView view) {
        super(view);
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void downLoadData() {
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
//        String currentUserId = user.getObjectId();

        AVQuery<AddressEntity> addressEntityAVQuery = new AVQuery<>("user_address");
        addressEntityAVQuery.whereEqualTo("ownerObjectId", user);

        addressEntityAVQuery.orderByAscending("isfirst");
        addressEntityAVQuery.findInBackground(new FindCallback<AddressEntity>() {
            @Override
            public void done(List<AddressEntity> list, AVException e) {
                if (e == null) {
                    AddressEntity oneFirst = new AddressEntity();
                    for (AddressEntity addressEntity : list) {
                        if (addressEntity.getIsfirst().equals("1")) {
                            if (TextUtils.isEmpty(oneFirst.getIsfirst())) {
                                oneFirst = addressEntity;
                            } else {
                                if (oneFirst.getUpdatedAt() != null ) {
                                    boolean one = oneFirst.getUpdatedAt().after(addressEntity.getUpdatedAt());
                                    if (one) {
                                        addressEntity.setIsfirst("2");
                                    } else {
                                        oneFirst.setIsfirst("2");
                                    }
                                }
                            }
                        }
                    }
                    List<AddressEntity> todos = list;
                    try {
                        AVObject.saveAll(todos);
                    } catch (AVException e1) {
                        e1.printStackTrace();
                    }
                    mView.setData(list);
                } else {
                    mView.setData(null);
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void upLoadIsFirstChange(List<AddressEntity> list) {

    }
}
